 /* VLC Player
 * VlcPlayer.cs
 * 
 * This program will load a list of satelite channels that will play
 * in the program via VLC player
 * 
 * @author: Brian Jacobe
 * @ver: 1.0
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AxAXVLC;
using System.IO;

namespace VLC_Player
{
    public partial class VlcPlayer : Form
    {
        System.IO.StreamReader file = new System.IO.StreamReader("config.txt");
        
        public VlcPlayer()
        {
            InitializeComponent();
            Image postmodern = Image.FromFile("postmodern.png");
            pictureBox1.Image = postmodern;
            pictureBox1.Height = postmodern.Height;
            pictureBox1.Width = postmodern.Width;

            int counter = 0;
            string channel;

            //Grab data from config.txt and load them into the listbox
            while ((channel = file.ReadLine()) != null)
            {
                listBox1.Items.Insert(counter, channel);                
                counter++;
            }
            file.Close();
            
            //Event handler for selected item in listbox
            this.listBox1.SelectedItem = new EventHandler(listBox1_SelectedIndexChanged);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string channelSelect = listBox1.SelectedItem.ToString();
                int indexChannel = listBox1.FindString(channelSelect);
                if (indexChannel == -1 || channelSelect == null)
                {
                    MessageBox.Show("Item is not available");
                }
                else
                {
                    string[] splitPath = channelSelect.Split('\t');
                    string storeFile = (@"file:///" + splitPath[2]);
                   
                    vlc.addTarget(@storeFile, null, AXVLC.VLCPlaylistMode.VLCPlayListInsertAndGo, 0);
                    vlc.play();
                    vlc.playlistClear();
                }
            }
            catch (Exception errorMessage)
            {
                Console.Write(errorMessage);
            }
        }
    }
}
